//
//  HomeViewController.swift
//  WeiboSwift
//
//  Created by xiaokui wu on 9/9/16.
//  Copyright © 2016 wxk. All rights reserved.
//

import UIKit

class HomeViewController: BaseTableViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin{
            visitorView?.setupVisitorInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        // 导航条
        setupNav()
        
        setupNotify()
    }
    
    private func setupNotify()
    {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(titleArrowChange), name: XKPresentationManagerDidPresentedController, object: transitionManager)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(titleArrowChange), name: XKPresentationManagerDidDismissedController, object: transitionManager)
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    @objc private func titleArrowChange()
    {
        titleButton.selected = !titleButton.selected
    }
    
    private func setupNav()
    {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        
        
        navigationItem.titleView = titleButton
    }
    
    // MARK: - event
    @objc private func titleBtnClick(btn: TitleButton)
    {
        guard let popMenuVC = UIStoryboard(name: "PopMenu", bundle: nil).instantiateInitialViewController() else{
        
            return
        }
        
        // 自定义转场动画
        popMenuVC.transitioningDelegate = transitionManager
        popMenuVC.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(popMenuVC, animated: true, completion: nil)
        
    }
    
    @objc private func leftBtnClick()
    {
        Log("")
    }
    
    @objc private func rightBtnClick()
    {
        Log("")
        if let qrVC = UIStoryboard(name: "QRCode", bundle: nil).instantiateInitialViewController(){
            presentViewController(qrVC, animated: true, completion: nil)
        }
    }

    // MARK: - lazy
    private lazy var transitionManager : XKPresentationManager = {
            let manager = XKPresentationManager()
            manager.presentedViewFrame = CGRect(x: 100, y: 45, width: 200, height: 400)
            return manager
        }()
    private lazy var titleButton: TitleButton = {
        let btn = TitleButton()
        btn.setTitle("奎哥的微博", forState: .Normal)
        btn.addTarget(self, action: #selector(HomeViewController.titleBtnClick(_:)), forControlEvents: .TouchUpInside)
        return btn
    }()
}

